package org.solr;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;

import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.common.SolrInputDocument;

public class IndexDic {
	public static int num, wordcount = 0;

	public static int maxlen = 0;
	static CloudSolrServer server;
	static String path = "D:/sougo/dic";

	// 非递归
	public static void scanDirNoRecursion(String path) throws Exception {
		LinkedList<File> list = new LinkedList<File>();
		File dir = new File(path);
		File file[] = dir.listFiles();
		for (int i = 0; i < file.length; i++) {
			if (file[i].isDirectory())
				list.add(file[i]);
			else {
				read(file[i].getAbsolutePath());
				num++;
			}
		}
		File tmp;
		while (!list.isEmpty()) {
			tmp = (File) list.removeFirst();// 首个目录
			if (tmp.isDirectory()) {
				file = tmp.listFiles();
				if (file == null)
					continue;
				for (int i = 0; i < file.length; i++) {
					if (file[i].isDirectory())
						list.add(file[i]);// 目录则加入目录列表，关键
					else {
						System.out.println(file[i]);
						num++;
					}
				}
			} else {
				System.out.println(tmp);
				num++;
			}
		}
	}

	// 递归
	public static void scanDirRecursion(File file) {
		try {
			if (file.canRead()) {
				if (file.isDirectory()) {
					String[] files = file.list();
					if (files != null) {
						for (int i = 0; i < files.length; i++) {
							scanDirRecursion(new File(file, files[i]));
						}
					}
				} else {
					// if (file.getName().endsWith("ppt"))
					System.out.println(file);
					num++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void read(String filePath) throws Exception {
		String line;
		String[] word;
		String[] type;

		int count;
		long tagTime = System.currentTimeMillis();
		try {
			BufferedReader br = new BufferedReader(new FileReader(filePath));
			line = br.readLine();
			count = 0;
			Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
			while (line != null) {
				word = line.split("[  	]");
				SolrInputDocument doc = new SolrInputDocument();
				if (word[0].length() > maxlen)
					maxlen = word[0].length();
				doc.addField("id", word[0]);
				if (word.length > 1) {
					doc.addField("freq", word[1]);
				}
				if (word.length > 2) {
					type = word[2].split(",");
					for (int j = 0; j < type.length; j++)
						doc.addField("type", type[j]);
				}
				doc.addField("dict", filePath.replace("D:\\sougo\\dic\\", ""));
				docs.add(doc);
				count++;
				wordcount++;
				line = br.readLine();
			}
			if (docs.size() > 0) {
				server.add(docs);
			}
			br.close();
			System.out.format("add %s words use %s ms\n", count,
					(System.currentTimeMillis() - tagTime));
			docs.clear();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] argc) throws Exception {
		long a = System.currentTimeMillis();
		num = 0;
		server = new CloudSolrServer("localhost:2181");
		server.setDefaultCollection("collection1");
		scanDirNoRecursion(path);
		System.out.println("最长词语:" + maxlen);
		System.out.println("词语总数:" + wordcount);
		System.out.println("文件总数:" + num);
		System.out.print("总耗时:");
		System.out.println(System.currentTimeMillis() - a);
	}
}
